﻿using System.Collections.Generic;
using UnityEditor;
using UnityEngine;

namespace CWM.Skinn
{
    public static partial class SkinnEditorEx
    {
        public static TextureRefrence[] GetAllPropertiesWithTextures(this Material material, ref List<string> propertyNames)
        {
            if (material == null || material.shader == null) return new TextureRefrence[0];
            if (propertyNames == null) propertyNames = new List<string>();
            var maxTextures = 64;
            var textures = new TextureRefrence[maxTextures];
            var shader = material.shader;
            int propertyCount = ShaderUtil.GetPropertyCount(shader);
            int textureCount = 0;
            for (int i = 0; i < propertyCount; i++)
            {
                if (textureCount >= maxTextures) break;
                if (ShaderUtil.GetPropertyType(shader, i) != ShaderUtil.ShaderPropertyType.TexEnv) continue;
                var propertyName =  ShaderUtil.GetPropertyName(shader, i);
                var texture = material.GetTexture(propertyName);
                if (!texture) continue;
                textures[textureCount] = new TextureRefrence() {textureName = texture.name, texture = texture, propertyName = propertyName };
                if (!propertyNames.Contains(propertyName)) propertyNames.Add(propertyName);
                textureCount++;
            }

            if (propertyNames.Count < 1) propertyNames = new List<string>() { "no textures found" };
            if (textureCount < 1) return  new TextureRefrence[0];
            System.Array.Resize(ref textures, textureCount);
            return textures;
        }

        public static string[] GetAllPropertiesWithTextures(this Material material)
        {
            if (material == null || material.shader == null) return new string[0];
            List<string> propertyNames = new List<string>();
            var maxTextures = 64;
            var textures = new TextureRefrence[maxTextures];
            var shader = material.shader;
            int propertyCount = ShaderUtil.GetPropertyCount(shader);
            int textureCount = 0;
            for (int i = 0; i < propertyCount; i++)
            {
                if (textureCount >= maxTextures) break;
                if (ShaderUtil.GetPropertyType(shader, i) != ShaderUtil.ShaderPropertyType.TexEnv) continue;
                var propertyName = ShaderUtil.GetPropertyName(shader, i);
                var texture = material.GetTexture(propertyName);
                if (!texture) continue;
                textures[textureCount] = new TextureRefrence() { textureName = texture.name, texture = texture, propertyName = propertyName };
                propertyNames.Add(propertyName);
                textureCount++;
            }
            return propertyNames.ToArray();
        }

        public static string GetColorProperty(this Material material)
        {
            var propertyName = "";
            if (material == null || material.shader == null) return propertyName;
            var shader = material.shader;
            int propertyCount = ShaderUtil.GetPropertyCount(shader);
            for (int i = 0; i < propertyCount; i++)
            {
                if (ShaderUtil.GetPropertyType(shader, i) != ShaderUtil.ShaderPropertyType.Color) continue;
                 propertyName = ShaderUtil.GetPropertyName(shader, i);
                if (propertyName == "_Color" || propertyName == "_MainColor") return propertyName;
            }
            return propertyName;
        }
    }
}
